package com.accp.taobao.service;

import java.io.*;
import java.util.HashMap;

/**
 * 用于读入原文本的线程
 */
public class ReadFileThread extends Thread{
    private File file;
    private int group;
    private int interval;
    private HashMap<Integer,String> dataGroups;

    public ReadFileThread(File file, int group, int interval, HashMap<Integer,String> dataGroups) {
        this.file = file;
        this.group = group;
        this.interval = interval;
        this.dataGroups = dataGroups;
    }

    @Override
    public void run() {
        InputStream is=null;
        DataInputStream dis=null;
        try {
            is=new FileInputStream(file);
            dis=new DataInputStream(is);
            int start = interval*group;
            int end = interval*(group+1);
            if(group==9) end=(int)file.length();
            byte [] temp=new byte[end-start];
            dis.skipBytes(start);
            for (int i = start,k=0; i < end; i++) {
                temp[k++]=dis.readByte();
            }
            String s=new String(temp);
            dataGroups.put(group,s);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                dis.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
